package com.carsystems.controller;

import com.carsystems.mapper.AdminUserMapper;
import com.carsystems.pojo.AdminUser;
import com.carsystems.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.omg.CORBA.Object;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@CrossOrigin
@RestController
public class AdminUserController {

    @Autowired
    AdminUserMapper adminUserMapper;

    @Autowired
    AdminService adminService;
//    用户登陆
    @PostMapping("/adminlogin")
    public Map<String, java.lang.Object> adminlogin(@RequestBody AdminUser adminUser){
        System.out.println(adminUser.toString()+"这是登陆用户的信息");
       Map<String,java.lang.Object> map = new HashMap<>();
       try {
           AdminUser adminDB = adminService.login(adminUser);
           map.put("state",true);
           map.put("msg","登陆成功");
       }catch (Exception e){
           e.printStackTrace();
           map.put("state",false);
           map.put("msg",e.getMessage());
       }
       return map;
    }


    //查看全部
    @GetMapping("/queradminUserList")
    public List<AdminUser> queradminUserList() {
        List<AdminUser> adminUsers = adminUserMapper.queradminUserList();
        return adminUsers;
    }

    //按照_id查询
    @GetMapping("/queryByPrimaryKey")
    public AdminUser queradminUserByPrimaryKey(Integer admin_id) {
        AdminUser adminUser = adminUserMapper.queradminUserByPrimaryKey(admin_id);
        return adminUserMapper.queradminUserByPrimaryKey(admin_id);
    }

    //增加一个
    @ResponseBody
    @PostMapping("/addAdminUser")
    public String addAdminUser(@RequestBody AdminUser adminUser) {
        adminUserMapper.addAdminUser(adminUser);
        if (adminUser != null) {
            return "success";
        } else {
            return "fail";
        }
    }

    //修改信息
    @ResponseBody
    @PutMapping("/updateAdminUser")
    public String updateAdminUser(@RequestBody AdminUser adminUser){
        int row = adminUserMapper.updateAdminUser(adminUser);
        adminUserMapper.queradminUserByPrimaryKey(adminUser.getAdmin_id());
        if (row==1){
            return "success";
        }else {
            return "fail";
        }
    }

    //删除公司
    @ResponseBody
    @DeleteMapping("/deleteAdminUser")
    public String deleteAdminUser(AdminUser adminUser){
        int row = adminUserMapper.deleteAdminUser(adminUser.getAdmin_id());
        if (row == 1){
            return "success";
        }else {
            return "fail";
        }
    }

}
